#!/usr/bin/env python3
# -*- coding: utf-8 -*-

concur_num = [1, 2, 3]
idx_match = 0
idx_display = 1

data_in_files = {
# seq: [match_name, display_name]
1: ["process num=",   "process"],
2: ["thread num=",    "thread"]
}
data_output = {}
data_show = {}

def save_data(_data, num, line):
    _key = "{0}_num_{1}".format(_data[idx_display], num * 40)
    _time = line.split()[1]
    _type = "save"
    if _key not in data_output:
        data_output[_key] = []
        _type = "new "
    data_output[_key].append(_time)
    if debug_level is True:
        print("[{2}] {0:>12} {1}".format( _key, _time, _type))
        
def get_hackbench_data(file):
    header_found = False
    for seq in data_in_files:
        _data = data_in_files[seq]
        for num in concur_num:
            _match_str = "{0}{1}".format(_data[idx_match], num)
            _run_info = "Running with {0}*40 (== {1}) tasks.".format(num, num * 40)
            if debug_level is True:
                print("match string: {0}".format(_match_str))
                print("run   string: {0}".format(_run_info))
            with open(file) as f:
                for line in f:
                    line = line.strip().strip('\n')
                    if header_found is True:
                        if "num=" in line:
                            header_found = False
                            break

                        if "Time: " in line:
                            save_data(_data, num, line)
                            continue

                    if _match_str in line:
                        header_found = True
                        continue

def save_avg_data(key, avg):
    _type = "save"
    if key not in data_show:
        _type = "new "
        data_show[key] = []
    data_show[key].append(avg)

def show_data():
    if debug_level is True:
        print("\n-------- output single --------\n")
    for i in data_output:
        avg = 0
        total = 0
        _data = data_output[i]
        count = len(_data)
        for value in _data:
            total += float(value)
        avg = float(total / count)
        save_avg_data(i, avg)
        if debug_level is True:
            print("{0:>23} | {1} | avg: {2}".format(i, _data, avg))

def analyze_log(file):
    get_hackbench_data(file)
    show_data()
    data_output.clear()

def handler_data():
    if debug_level is True:
        print("\n-------- output all --------\n")
    for _type in data_show:
        avg = 0
        total = 0
        print("\n%-41s smaller_better " % (_type), end='')
        _data = data_show[_type]
        count = len(_data)
        for avg_value in _data:
            print(" %10.4f " % (avg_value), end='')
            total += float(avg_value)
        avg = float(total / count)
        print(" %10.4f " % (avg), end='')
        for i in _data:
            i_flux = float('%.0f' % (float(abs((float(i) - avg) * 100 / avg))))
            print(" %6.0f%%" % i_flux, end='')
    print("\n")

def init(dbg):
    global debug_level
    debug_level = dbg
    return analyze_log, handler_data